<template>
  <div class="login-container">
    <div class="login-box">
      <el-form ref="ruleFormRef" :model="loginForm" :rules="rules" label-width="auto" class="login-form">
        <el-form-item>
          <div class="logo-container">
            <img class="logo-img" src="@\assets\image\logo.png" alt="Company Logo">
          </div>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <div class="button-container">
            <el-button size="large" class="login-button" type="primary" @click="login">登录</el-button>
            <el-button size="large" class="login-button" @click="clean">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { loginApi } from '@/api/login';
import { ElMessage } from 'element-plus';
import router from '../../router';
const ruleFormRef = ref(null);
const loginForm = ref({
  username: '',
  password: '',
});

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
  ]
});

const login = async () => {
  ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      console.log('提交登录', loginForm);
      // 这里添加登录API调用
      const result = await loginApi(loginForm.value)
      if (result.code == 1) {
        ElMessage.success('登录成功')
        localStorage.setItem('loginInfo', JSON.stringify(result.data))
        router.push('/')
      } else {
        ElMessage.error(result.msg)
      }
    }
  });
};

const clean = () => {
  ruleFormRef.value.resetFields();
};
</script>

<style scoped>
body {
  padding: 0px !important;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("@/assets/image/login-background.png");
  background-repeat: no-repeat;
  background-size: cover;
}

.login-box {
  background: white;
  padding: 40px 30px 20px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  min-width: 320px;
}

.login-box:hover {
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
}

.login-form {
  width: 100%;
}

.button-container {
  display: flex;
  justify-content: center;
  /* 水平居中 */
  gap: 20px;
  /* 按钮之间的间距 */
  width: 100%;
}

.img-container {
  display: flex;
  justify-content: center;
}

.logo-container {
  display: flex;
  justify-content: center;
  /* 水平居中 */
  margin-bottom: 25px;
  /* 底部间距 */
  animation: float 4s ease-in-out infinite;
  /* 浮动动画 */
}

.logo-img {
  width: 240px;
  /* 图片宽度 */
  height: 60px;
  /* 图片高度 */
  object-fit: contain;
  /* 保持比例 */
  filter: drop-shadow(0 5px 15px rgba(26, 42, 108, 0.3));
  /* 阴影效果 */
}
</style>